/**
 * Module dependencies.
 */



var express = require('express');
var http = require('http');
var path = require('path');
var use = require('./routes/user');


var passport = require('passport');
var util = require('util')
var FacebookStrategy = require('passport-facebook').Strategy;

var FACEBOOK_APP_ID = "1397243627178114"
var FACEBOOK_APP_SECRET = "716ac1f8515660e967fd5c829f6c9c7d";


// Passport session setup.
//   To support persistent login sessions, Passport needs to be able to
//   serialize users into and deserialize users out of the session.  Typically,
//   this will be as simple as storing the user ID when serializing, and finding
//   the user by ID when deserializing.  However, since this example does not
//   have a database of user records, the complete Facebook profile is serialized
//   and deserialized.
passport.serializeUser(function(user, done) {
    done(null, user);
});

passport.deserializeUser(function(obj, done) {
    done(null, obj);
});

// Use the FacebookStrategy within Passport.
//   Strategies in Passport require a `verify` function, which accept
//   credentials (in this case, an accessToken, refreshToken, and Facebook
//   profile), and invoke a callback with a user object.
passport.use(new FacebookStrategy({
        clientID: FACEBOOK_APP_ID,
        clientSecret: FACEBOOK_APP_SECRET,
        callbackURL: "http://localhost:3000/auth/facebook/callback"
    },
    function(accessToken, refreshToken, profile, done) {
        // asynchronous verification, for effect...
        process.nextTick(function () {

            // To keep the example simple, the user's Facebook profile is returned to
            // represent the logged-in user.  In a typical application, you would want
            // to associate the Facebook account with a user record in your database,
            // and return that user instead.
            return done(null, profile);
        });
    }
));

var app = express();

// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.cookieParser('your secret here'));
app.use(express.session());
// Initialize Passport!  Also use passport.session() middleware, to support
// persistent login sessions (recommended).
app.use(passport.initialize());
app.use(passport.session());
app.use(app.router);
app.use(require('less-middleware')({ src: __dirname + '/public' }));
app.use(express.static(path.join(__dirname, 'public')));

// development only
if ('development' == app.get('env')) {

    app.use(express.errorHandler());
}

app.get('/', use.home);

app.get('/register', use.register);
app.post('/register', use.processRegister);

app.get('/users', use.user);
app.post('/', use.login);
app.post('/users', use.DatabaseLists);

app.get('/admin', use.admin);
app.post('/admin', use.DatabaseLists);

app.get('/logout', use.logout);

app.get('/testing', use.testing);

app.post('/request', use.request);

app.post('/requestGameStart', use.requestGameStart);

app.post('/requestGRUsers', use.requestGRUsers);

app.post('/requestGameEnd', use.requestGameEnd);

app.post('/requestGameState', use.requestGameState);

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

// GET /auth/facebook
//   Use passport.authenticate() as route middleware to authenticate the
//   request.  The first step in Facebook authentication will involve
//   redirecting the user to facebook.com.  After authorization, Facebook will
//   redirect the user back to this application at /auth/facebook/callback
app.get('/auth/facebook',
    passport.authenticate('facebook'),
    function(req, res){
        // The request will be redirected to Facebook for authentication, so this
        // function will not be called.
    });

app.get('/auth/facebook/callback',
    passport.authenticate('facebook', { failureRedirect: '/' }),

    function(req, res) {
        res.redirect('/FbLogin');

    });

app.get('/FbLogin', use.FbLogin);

http.createServer(app).listen(app.get('port'), function () {

    console.log('Express server listening on port ' + app.get('port'));

});
